var pageSize = 10;
var pageNum = 1;
var page;
var list;
var layer;
var index;
var laypage;

$(function () {
    $("#menuId").val(sessionStorage.menuId);
    layui.use(['laydate', 'element', 'laypage', 'layer'], function () {
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        lement = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层

        var start = {
            // min: laydate.now(),
            max: '2099-06-16 23:59:59'
            ,istoday: true
            ,choose: function(datas){
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };

        var end = {
            // min: laydate.now(),
            max: '2099-06-16 23:59:59'
            ,istoday: false
            ,choose: function(datas){
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };



        document.getElementById('LAY_demorange_s').onclick = function(){
            start.elem = this;
            laydate(start);
        }
        document.getElementById('LAY_demorange_e').onclick = function(){
            end.elem = this
            laydate(end);
        }


    });
    setTimeout(function () {
        advertList();
    },200);

});



function advertList() {
    var menuId = $("#menuId").val();
    var advertType = $("#advertType").val();
    var state = $("#state").val();
    var startTime = $("#LAY_demorange_s").val();
    var endTime = $("#LAY_demorange_e").val();
    var params = {
        "type": "query",
        "menuId": menuId,
        "pageSize": pageSize,
        "pageNum": pageNum,
        "advertType":advertType,
        "state":state,
        "startTime":startTime,
        "endTime":endTime
    };
    $.ajax({
        url: "/advert/list",
        type: "post",
        data: params,
        dataType: "json",
        success: function (data) {
            if (data.code = 1 && data.code != undefined) {
                list = data.list;
                page = data.page;
                var html = "";
                var redEnvelopeType = "";
                var statStr="";
                var advertType = "";

                for (var l in list) {
                    if(list[l].redEnvelopeType == 1){
                        redEnvelopeType = "拼手气";
                    }else if(list[l].redEnvelopeType == 2){
                        redEnvelopeType = "定额";
                    }

                    if(list[l].state==0){
                        statStr="待审核"
                    } else if(list[l].state==1){
                        statStr="投放中"
                    } else if(list[l].state==2){
                        statStr="暂停"
                    } else if(list[l].state==3){
                        statStr="已下线"
                    } else if(list[l].state==4){
                        statStr="审核未通过"
                    } else if(list[l].state==5){
                        statStr="超时完成"
                    }

                    if(list[l].advertType == -1){
                        advertType = "新用户福袋";
                    }else if(list[l].advertType == 0){
                        advertType = "普通福袋";
                    }else if(list[l].advertType == 1){
                        advertType = "优惠券福袋";
                    }else if(list[l].advertType == 2){
                        advertType = "公告";
                    }else if(list[l].advertType == 3){
                        advertType = "优惠券圈子";
                    }else if(list[l].advertType == 4){
                        advertType = "接力福袋(圈子)";
                    }else if(list[l].advertType == 5){
                        advertType = "首页广告(首页）";
                    }else if(list[l].advertType == 6){
                        advertType = "定时广告";
                    }else if(list[l].advertType == 7){
                        advertType = "运营活动广告(官方)";
                    }else if(list[l].advertType == 8){
                        advertType = "定时活动广告(官方)";
                    }else if(list[l].advertType == 9){
                        advertType = "说说(圈子)";
                    }else if(list[l].advertType == 10){
                        advertType = "公益广告";
                    }else if(list[l].advertType == 11){
                        advertType = "幸运转盘";
                    }else if(list[l].advertType == 20){
                        advertType = "好友临时福袋";
                    }
                    html += '<tr class="odd">' +
                        '<td>' + list[l].id + '</td>' +
                        '<td>' + advertType + '</td>' +
                        '<td>' + list[l].phone + '</td>' +
                        '<td>' + redEnvelopeType + '</td>' +
                        '<td>' + list[l].redEnvelopeCount + '</td>' +
                        '<td>' + list[l].redEnvelopeAmount /100 + '</td>' +
                        '<td>' + statStr + '</td>' +
                        '<td>' + (list[l].startTime == null ? "" : new Date(list[l].startTime).toLocaleString()) + '</td>' +
                        '<td>' + (list[l].auditTime == null ? "" : new Date(list[l].auditTime).toLocaleString()) + '</td>' +
                        '<td>' + (list[l].auditUserName == null ? "" : list[l].auditUserName) + '</td>' +
                        '<td><button class="layui-btn layui-btn-normal" onclick="findDetails('+l+')">查看详情</button>';
                    if(sessionStorage.getItem(menuId+'edit') == 1){
                        if(list[l].topFlag == 1){
                            html += '<button class="layui-btn layui-btn-normal" onclick="updateTopFlag(this,'+l+')">置顶</button>';
                        }else {
                            html += '<button class="layui-btn layui-btn-primary" onclick="updateTopFlag(this,'+l+')">置顶</button>';
                        }
                        if(list[l].state == 0){
                            html += '<button class="layui-btn layui-btn-normal" onclick="audit('+list[l].id+')">审核</button>';
                        }
                        if((list[l].state == 1 || list[l].state == 2) && list[l].advertType != 4 && list[l].advertType != 9){
                            html += '<button class="layui-btn layui-btn-normal" onclick="showIsOfflinePage('+list[l].id+')">下线广告</button>';
                        }
                    }

                    html += '</td></tr>';
                }
                $("#advertList2").html(html);
                $("#total").html("共有数据：" + page.total + " 条");
                laypage({
                    cont: 'page'
                    , pages: page.pages
                    , first: 1
                    ,curr:pageNum
                    , last: page.pages
                    , prev: '<em><</em>'
                    , next: '<em>></em>'
                    , jump: function (obj, first) {
                        pageNum = obj.curr;
                        if (!first) {
                            advertList();
                        }
                    }
                });
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
            }
        }
    })
}

//查询按钮
function search() {
    pageNum = 1;
    advertList();
}

//广告置顶
function updateTopFlag(obj,l) {
    var menuId = $("#menuId").val();
    var topFlag = list[l].topFlag;
    var id = list[l].id;
    if(topFlag == 0){
        topFlag = 1;
    }else {
        topFlag = 0;
    }
    $(obj).attr("class","layui-btn layui-btn-disabled");
    var params = {
        "type": "change",
        "menuId": menuId,
        "id":id,
        "topFlag":topFlag
    };
    $.ajax({
        url: "/advert/updateTopFlag",
        type: "post",
        data: params,
        dataType: "json",
        success:function (data) {
            if(data.code == 1 && data.code != undefined){
                if(data.topFlag == 1){
                    $(obj).attr("class","layui-btn layui-btn-normal");
                }else if(data.topFlag == 0){
                    $(obj).attr("class","layui-btn layui-btn-primary");
                }
                list[l].topFlag = data.topFlag;
            }else {
                if(list[l].topFlag == 1){
                    $(obj).attr("class","layui-btn layui-btn-normal");
                }else if(list[l].topFlag == 0){
                    $(obj).attr("class","layui-btn layui-btn-primary");
                }
            }
        }
    });
}

//查看详情
function findDetails(l){
    var advert = list[l];
    var labelType = "";
    if(list[l].labelType == 0){
        labelType = "美食";
    }else if(list[l].labelType == 1){
        labelType = "服装";
    }else if(list[l].labelType == 2){
        labelType = "生活";
    }
    else if(list[l].labelType == 3){
        labelType = "数码";
    }else if(list[l].labelType == 4){
        labelType = "信息";
    }else if(list[l].labelType == 5){
        labelType = "求职";
    }else if(list[l].labelType == 6){
        labelType = "吐槽";
    }else if(list[l].labelType == 7){
        labelType = "交友";
    }else if(list[l].labelType == 8){
        labelType = "跑腿";
    }else if(list[l].labelType == 9){
        labelType = "求助";
    }else if(list[l].labelType == 10){
        labelType = "顺风车";
    }else if(list[l].labelType == 11){
        labelType = "公告";
    }else if(list[l].labelType == 12){
        labelType = "其他";
    }else {
        labelType = "其他";
    }
    var content = JSON.parse(advert.advertContent);
    var html = '<div style="padding-left: 18px"><strong>福袋类型 :</strong> '+labelType+'<br/><br/>'+
        '<strong>福袋介绍 : </strong>'+content.content+'<br/><strong>图片/视频 : </strong>';
        for(var p in content.photo){
            html += '<img src="'+content.photo[p]+'" style="width: 100px;margin: 5px 5px">';
        }
        html += '<br/><br/><strong>投放类型 : </strong><br/> <br/>   '+
            '<strong>投放总金额 : </strong>'+advert.redEnvelopeAmount / 100+'<label style="margin-left: 100px"><strong>福袋数量 : </strong>'+advert.redEnvelopeCount+'</label><br/><br/>';
        var sex = "不限";
        if(advert.advertConditionSex == 0){
            sex = "男";
        }else if(advert.advertConditionSex == 1){
            sex = "女";
        }
        var married = "不限";
        if(advert.married == 1){
            married = "恋爱";
        }else if(advert.married == 2){
            married = "单身";
        }else if(advert.married == 3){
            married = "已婚";
        }
        var age = "不限";
        if(advert.advertConditionAgeScope == 1){
            age = "20~30岁";
        }else if(advert.advertConditionAgeScope == 2){
            age = "31~40岁";
        }else if(advert.advertConditionAgeScope == 3){
            age = "41~50岁";
        }else if(advert.advertConditionAgeScope == 4) {
            age = "51岁以上";
        }
        var hobby = "不限";
        if(advert.hobby == 1){
            hobby = "美妆";
        }else if(advert.hobby == 2){
            hobby = "汽车";
        }else if(advert.hobby == 3){
            hobby = "时尚";
        }else if(advert.hobby == 4){
            hobby = "旅游";
        }else if(advert.hobby == 5){
            hobby = "游戏";
        }else if(advert.hobby == 6){
            hobby = "动漫";
        }else if(advert.hobby == 7){
            hobby = "音乐";
        }else if(advert.hobby == 8){
            hobby = "美食";
        }else if(advert.hobby == 9){
            hobby = "艺术";
        }else if(advert.hobby == 10){
            hobby = "摄影";
        }else if(advert.hobby == 11){
            hobby = "读书";
        }else if(advert.hobby == 12){
            hobby = "社交";
        }else if(advert.hobby == 13){
            hobby = "运动";
        }else if(advert.hobby == 14){
            hobby = "影视";
        }else if(advert.hobby == 15){
            hobby = "极客";
        }
        var work = "不限";
        if(advert.work == 1){
            work = "律师";
        }else if(advert.work == 2){
            work = "工程师";
        }else if(advert.work == 3){
            work = "的哥";
        }else if(advert.work == 4){
            work = "医生";
        }else if(advert.work == 5){
            work = "IT";
        }else if(advert.work == 6){
            work = "自由职业";
        }else if(advert.work == 7){
            work = "公务员";
        }else if(advert.work == 8){
            work = "企业管理";
        }else if(advert.work == 9){
            work = "教师";
        }else if(advert.work == 10){
            work = "学生";
        }else if(advert.work == 11){
            work = "个体老板";
        }
        var distance = "";
        if(advert.advertConditionDistance == 0){
            distance = "1公里";
        }else if(advert.advertConditionDistance == 1){
            distance = "3公里";
        }else if(advert.advertConditionDistance == 2){
            distance = "5公里";
        }else if(advert.advertConditionDistance == 3){
            distance = "8公里";
        }else if(advert.advertConditionDistance == 4){
            distance = "10公里";
        }
        html += '<label style="width: 191px;display: inline-block"><strong>性别 : </strong>'+sex+'</label><label style=""><strong>感情状况 : </strong>'+married+'</label><label style="margin-left: 100px"><strong>年龄 : </strong>'+age+'</label><br/><br/>'+
            '<label style="width: 191px;display: inline-block"><strong>爱好 : </strong>'+hobby+'</label><label style=""><strong>职业 : </strong>'+work+'</label><label style="margin-left: 128px"><strong>区域 : </strong>'+distance+'</label>';


        html += '</div>'
    layer.open({
        title: ['广告详情','color: #2299ee;border-bottom: 1px solid;']
        ,type: 1
        ,content: html
        ,area: ['60%', '60%']
        ,maxmin:true
    });
}

//广告审核
function audit(id) {
    var html = "<button id='bt1' onclick='through("+id+")' class='layui-btn layui-btn-normal' style='margin-left: 100px !important;margin-top: 35px !important;'>通过</button><button id='bt2' onclick='noThrough("+id+")' class='layui-btn layui-btn-danger' style='margin-left: 100px !important;margin-top: 35px !important;'>不通过</button>"
    index = layer.open({
        title: ['广告审核','color: #2299ee;border-bottom: 1px solid;']
        ,type: 1
        ,content: html
        ,area: ['390px', '130px']
        ,maxmin:true
    });
}
//审核通过
function through(id) {
    $("#bt1").attr("disabled",true);
    $("#bt1").attr("class","layui-btn layui-btn-disabled");
    $("#bt2").attr("disabled",true);
    $("#bt2").attr("class","layui-btn layui-btn-disabled");
    var menuId = $("#menuId").val();
    var params = {
        "type":"change",
        "menuId":menuId,
        "id":id
    };
    $.ajax({
        type:"post",
        url:"/advert/through",
        data:params,
        dataType:"json" ,
        success :function (data) {

            if(data.code == 1 && data.code != undefined){
                alert("审核成功");
                layer.close(index);
                advertList();
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
                layer.close(index);
                advertList();
            }else if(data.code == 2 && data.code != undefined){
                alert("审核失败");
                layer.close(index);
                advertList();
            }else if(data.code == 3 && data.code != undefined){
                alert("系统异常,请联系管理员");
                layer.close(index);
                advertList();
            }

        }
    });
}
//审核不通过
function noThrough(id) {
    $("#bt1").attr("disabled",true);
    $("#bt1").attr("class","layui-btn layui-btn-disabled");
    $("#bt2").attr("disabled",true);
    $("#bt2").attr("class","layui-btn layui-btn-disabled");
    var menuId = $("#menuId").val();
    var params = {
        "type":"change",
        "menuId":menuId,
        "id":id
    };
    $.ajax({
        type:"post",
        url:"/advert/noThrough",
        data:params,
        dataType:"json" ,
        success :function (data) {

            if(data.code == 1 && data.code != undefined){
                alert("审核成功");
                layer.close(index);
                advertList();
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
                layer.close(index);
                advertList();
            }else if(data.code == 2 && data.code != undefined){
                alert("审核失败");
                layer.close(index);
                advertList();
            }else if(data.code == 3 && data.code != undefined){
                alert("系统异常,请联系管理员");
                layer.close(index);
                advertList();
            }
        }
    });
}
//打开是否下线页面
var offlineAdvertIndex;
function showIsOfflinePage(id) {
    var menuId = $("#menuId").val();
    var params = {
        "type":"query",
        "menuId":menuId,
        "id":id
    };
    $.ajax({
        type:"post",
        url:"/advert/findBalance",
        data:params,
        dataType:"json" ,
        success :function (data) {
            console.log(data);
            if(data.code == 1 && data.code != undefined){
                var html = "<div style='text-align: center'><p style='margin-bottom: 30px;margin-top: 20px'>下线会将该广告中剩余广告币<span style='color: red'>"+data.balance+"(个)</span>全部退还给用户，确定要下线吗？</p><button onclick='offlineAdvert("+id+","+1+",this)' class='layui-btn layui-btn-normal'>确定</button><button onclick='offlineAdvert("+id+","+2+",this)' class='layui-btn layui-btn-normal' style='background-color: #ff5722;margin-left: 100px'>取消</button></div>"
                offlineAdvertIndex = layer.open({
                    title: ['提示','color: #2299ee;border-bottom: 1px solid;']
                    ,type: 1
                    ,content: html
                    ,area: ['500px', '200px']
                    ,maxmin:true
                });
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
                advertList();
            }else if(data.code == 2 && data.code != undefined){
                alert(data.message);
                advertList();
            }else if(data.code == 3 && data.code != undefined){
                alert("系统异常,请联系管理员");
                advertList();
            }
        }
    });
}

//下线广告
function offlineAdvert(id,index,obj) {
    $(obj).attr("disabled",true);
    if(index == 2){
        layer.close(offlineAdvertIndex);
        return false;
    }
    var menuId = $("#menuId").val();
    var params = {
        "type":"change",
        "menuId":menuId,
        "id":id
    };
    $.ajax({
        type:"post",
        url:"/advert/offlineAdvert",
        data:params,
        dataType:"json" ,
        success :function (data) {

            if(data.code == 1 && data.code != undefined){
                alert("下线成功");
                layer.close(offlineAdvertIndex);
                advertList();
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
                $(obj).attr("disabled",false);
                layer.close(offlineAdvertIndex);
                advertList();
            }else if(data.code == 2 && data.code != undefined){
                alert("下线失败");
                $(obj).attr("disabled",false);
                layer.close(offlineAdvertIndex);
                advertList();
            }else if(data.code == 3 && data.code != undefined){
                alert("系统异常,请联系管理员");
                layer.close(offlineAdvertIndex);
                advertList();
            }
        }
    });
}